<?php
namespace app\common\service;

use Symfony\Component\Intl\Languages;
use Symfony\Component\Intl\Countries;
use Symfony\Component\Intl\Locales;
use Symfony\Component\Intl\Locale;
use app\common\model\CountryLangCode;

 class Servicebase {

    //国家详情(获取国家名称)
    public function CountryDetail($arr = []){
        if(count($arr) < 1){return false;}
        \Locale::setDefault($arr['country_lang_code']);
        $country = null;
        $country['country_name'] = Locale::getDisplayRegion($arr['country_lang_code']);
        return $country;
    }

    //语言详情(获取语言名称)
    public function LangDetail($arr = []){
        if(!empty($arr['country_lang_code'])){
            \Locale::setDefault($arr['country_lang_code']);
            $reLang = [];
            $reLang['lang'] = Languages::getName($arr['country_lang_code']);
            return $reLang;
        }else{
            return false;
        }
    }

    //国家编码 语言编码
    public function CountryCodeDetail($arr = []){
        if(count($arr) < 1){return false;}
        $code = new CountryLangCode();
        $reCode = $code->where($arr)->find();
        if(!empty($reCode)){
            return $reCode->toArray();
        }
        return false;
    }

    //根据编码获取 语言 国家 名称
    public function CountryLangDetail($arr = []){

        if(count($arr) < 1){return false;}
        $reVal = [];
        $reCode = [];
        $reCountry = [];
        $reLang = [];
        $reCode = self::CountryCodeDetail($arr);
        if(!empty($reCode['country_lang_code'])){
            $reCountry = self::CountryDetail(['country_lang_code' => $reCode['country_lang_code']]);
            if(!empty($reCountry['country_name'])){
                $reVal['country_name'] = $reCountry['country_name'];
            }

            $reLang = self::LangDetail(['country_lang_code' => $reCode['country_lang_code']]);
            if(!empty($reLang['lang'])){
                $reVal['lang'] = $reLang['lang'];
            }
            $reVal = array_merge($reVal, $reCode);
        }else{
            $reVal = $reCode;
        }
        return $reVal;
    }

    //系统Banner类型
    public static function BannerType($lang = ''){

        $arr['zh'] = [
            'home'=>'首页',
            'federation_introduction'=>'联合会介绍',
            'purchase'=>'国际采购',
            'supply'=>'供应',
            'line_exhibition_hall'=>'线上展厅',
            'merchant_list'=>'商家列表',
            'product_video'=>'商品视频',
            'collaborative_institution'=>'合作机构'
        ];
        $arr['en'] = [
            'home'=>'Home',
            'federation_introduction'=>'Introduction to the Federation',
            'purchase'=>'International procurement',
            'supply'=>'Domestic supply',
            'line_exhibition_hall'=>'Online Exhibition Hall',
            'merchant_list'=>'Merchant List',
            'product_video'=>'Product video',
            'collaborative_institution'=>'Collaborative institutions'
        ];
        if($lang == ''){
            return $arr;
        }else{
            return $arr[$lang];
        }
    }

    //根据区域语言解析默认国家语言编码
    public function CountryLangCode($lang = '', $arr = []){

        $langCode = 'zh';
        $countryLangCode = 'zh_cn';
        if(!empty($lang)){
            switch($lang){
                case 'en' :
                    $langCode = 'en';
                    $countryLangCode = 'en_us';
                    break;

                case 'zh' :
                    $langCode = 'zh';
                    $countryLangCode = 'zh_cn';
                    break;

                default:
                    $langCode = 'zh';
                    $countryLangCode = 'zh_cn';
                    break;
            }
        }
        return ['lang_code'=>$langCode, 'country_lang_code'=>$countryLangCode];
    }
}